package com.ruoyi.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ai.domain.WxModelConfig;
import com.ruoyi.ai.domain.bo.WxModelConfigBo;
import com.ruoyi.ai.domain.vo.WxModelConfigVo;
import com.ruoyi.ai.factory.ModelChatStrategyFactory;
import com.ruoyi.ai.mapper.WxModelConfigMapper;
import com.ruoyi.ai.service.IWxModelConfigService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 模型配置Service业务层处理
 *
 * @author yh
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class WxModelConfigServiceImpl implements IWxModelConfigService {

    private final WxModelConfigMapper baseMapper;

    @PostConstruct
    public void init() {
        this.afterChange();
    }

    /**
     * 查询模型配置
     */
    @Override
    public WxModelConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public WxModelConfigVo queryByMfg(String mfg) {
        LambdaQueryWrapper<WxModelConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WxModelConfig::getMfg, mfg);
        List<WxModelConfigVo> wxModelConfigVos = baseMapper.selectVoList(lqw);
        if (CollectionUtils.isEmpty(wxModelConfigVos)) {
            return null;
        }
        return wxModelConfigVos.get(0);
    }

    /**
     * 查询模型配置列表
     */
    @Override
    public TableDataInfo<WxModelConfigVo> queryPageList(WxModelConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WxModelConfig> lqw = buildQueryWrapper(bo);
        Page<WxModelConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询模型配置列表
     */
    @Override
    public List<WxModelConfigVo> queryList(WxModelConfigBo bo) {
        LambdaQueryWrapper<WxModelConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WxModelConfig> buildQueryWrapper(WxModelConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WxModelConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMfg()), WxModelConfig::getMfg, bo.getMfg());
        lqw.eq(StringUtils.isNotBlank(bo.getModelType()), WxModelConfig::getModelType, bo.getModelType());
        lqw.eq(bo.getEnable() != null, WxModelConfig::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增模型配置
     */
    @Override
    public Boolean insertByBo(WxModelConfigBo bo) {
        WxModelConfig add = BeanUtil.toBean(bo, WxModelConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        afterChange();
        return flag;
    }

    /**
     * 修改模型配置
     */
    @Override
    public Boolean updateByBo(WxModelConfigBo bo) {
        WxModelConfig update = BeanUtil.toBean(bo, WxModelConfig.class);
        validEntityBeforeSave(update);
        if (bo.getEnable()!=null && bo.getEnable() == 1) {
            //其他的都变成0
            baseMapper.update(new LambdaUpdateWrapper<WxModelConfig>()
                .ne(WxModelConfig::getId, bo.getId())
                .set(WxModelConfig::getEnable, 0));
        }
        boolean result = baseMapper.updateById(update) > 0;
        afterChange();
        return result;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WxModelConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    private void afterChange() {
        List<WxModelConfigVo> wxModelConfigVos = queryList(new WxModelConfigBo());
        ModelChatStrategyFactory.updateModelCache(wxModelConfigVos);
    }

    /**
     * 批量删除模型配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        afterChange();
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
